﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//    TimeControlTest.cs
//
// Abstract:
//    
//    Unit tests for the time control.
//
// Classes:
//
//    Microsoft.EnhancedReminders.OutlookAddIn.UnitTests.TimeControlUnitTest
//
//--

using System.Windows;
using System.Windows.Shapes;
using System.Collections;
using Microsoft.EnhancedReminders.OutlookAddIn;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.EnhancedReminders.OutlookAddIn.UnitTests
{    
    /// <summary>
    ///This is a test class for TimeControlTest and is intended
    ///to contain all TimeControlTest Unit Tests
    ///</summary>
    [TestClass()]
    public class TimeControlTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        /// Verify initialization of the time control.
        /// </summary>
        [TestMethod]
        public void InitializeTimeGrid()
        {
            TimeControl_Accessor target = new TimeControl_Accessor();

            // Create a time bit array with bits 7 and 234 arbitrarily "on".
            Assert.IsTrue(target.NumCells > 234, "Unexpected cell count");
            BitArray times = new BitArray(target.NumCells);
            times.SetAll(false);
            times[7] = true;
            times[234] = true;

            // Initialize the time control.
            target.Initialize(times);

            // Verify it has filled in the correct cells.
            Assert.IsNotNull(target._cells, "_cells");
            Assert.AreEqual(target._cells.Count, times.Count, "_cells.Count");

            for (int i = 0; i < times.Count; i++)
            {
                Assert.AreEqual(times[i], target._cells[i].Fill == target.CellOn, "Cell not initialised correctly");
            }
        }

        /// <summary>
        /// Verify a random co-ordinate (101, 202) on the grid is mapped to the correct cell index.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Microsoft.EnhancedReminders.OutlookAddIn.dll")]
        public void GetIndexFromPositionTest()
        {
            TimeControl_Accessor target = new TimeControl_Accessor();
            
            // Force the size to a fixed width and height
            target.grid.RenderSize = new Size(/*width*/600, /*height*/400);

            Point positionInGrid = new Point(/*X*/101, /*Y*/202); // An arbitrary point.

            int expectedRow = (int)((202.0 / (400.0 / (double)target.grid.RowDefinitions.Count))) - 1;
            int expectedColumn = (int)((101.0 / (600.0 / (double)target.grid.ColumnDefinitions.Count))) - 1;
            int expectedIndex = expectedColumn * target.NumRows + expectedRow;

            int timeIndex = target.GetTimeIndexFromPosition(positionInGrid);

            Assert.AreEqual(expectedIndex, timeIndex, "Unexpected index from co-ordinate");
        }

        /// <summary>
        /// Verify a co-ordinate outside the grid has a cell index -1.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Microsoft.EnhancedReminders.OutlookAddIn.dll")]
        public void GetIndexWhenOutsideOfGridTest()
        {
            TimeControl_Accessor target = new TimeControl_Accessor();

            // Force the size to a fixed width and height
            target.grid.RenderSize = new Size(/*width*/600, /*height*/400);

            Point positionInGrid = new Point(/*X*/123, /*Y*/456); // A point outside the grid.

            Assert.AreEqual(-1, target.GetTimeIndexFromPosition(positionInGrid), "Unexpected index from co-ordinate");
        }

        /// <summary>
        /// Verify the co-ordinate (0, 0) which is in the header region maps to -1.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Microsoft.EnhancedReminders.OutlookAddIn.dll")]
        public void GetIndexFromPositionTest2()
        {
            TimeControl_Accessor target = new TimeControl_Accessor();

            target.grid.RenderSize = new Size(/*width*/600, /*height*/400);

            Point positionInGrid = new Point(/*X*/0, /*Y*/0);

            int timeIndex = target.GetTimeIndexFromPosition(positionInGrid);

            Assert.AreEqual(-1, timeIndex, "Unexpected time index");
        }

        /// <summary>
        /// Verify the bottom right corner point on the grid maps to the last index for time array.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Microsoft.EnhancedReminders.OutlookAddIn.dll")]
        public void GetIndexFromPositionTest3()
        {
            TimeControl_Accessor target = new TimeControl_Accessor();

            target.grid.RenderSize = new Size(/*width*/600, /*height*/400);

            Point positionInGrid = new Point(/*X*/599, /*Y*/399); // In the bottom-right-most cell.

            int timeIndex = target.GetTimeIndexFromPosition(positionInGrid);

            int expectedIndex = target.NumRows * target.NumColumns - 1;

            Assert.AreEqual(expectedIndex, timeIndex, "Unexpected index");
        }
    }
}
